package com.iweb.controller;

import com.iweb.annotation.TokenAuthentication;
import com.iweb.comment.pojo.Comment;
import com.iweb.comment.service.CommentService;
import com.iweb.common.page.DataGrid;
import com.iweb.common.result.EmptyObject;
import com.iweb.common.result.ResultEntity;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("comment")
public class CommentController {

    @Autowired
    protected CommentService commentService;

    @ApiOperation("根据文章的ID分页查询评论")
    @GetMapping("findCommentsByArticleId/{articleId}/{current}/{size}")
    public ResultEntity<DataGrid<Comment>> findCommentsByArticleId(@PathVariable String articleId,
                                                                   @PathVariable Integer current,
                                                                   @PathVariable Integer size) {
        DataGrid<Comment> dataGrid = commentService.findCommentsByArticleId(articleId, current,
                size);
        return ResultEntity.SUCCESS(dataGrid);
    }

    @TokenAuthentication
    @ApiOperation("新增评论")
    @PostMapping()
    public ResultEntity<Comment> addComment(@RequestBody Comment comment) {
        commentService.addComment(comment);
        return ResultEntity.SUCCESS(comment);
    }

    @TokenAuthentication
    @ApiOperation("根据评论ID修改评论内容")
    @PutMapping("{commentId}")
    public ResultEntity<Comment> updateComment(@PathVariable String commentId,
                                               @RequestBody Comment comment) {
        comment.set_id(commentId);
        Comment commentResult = commentService.updateComment(comment);
        return ResultEntity.SUCCESS(commentResult);
    }

    @TokenAuthentication
    @ApiOperation("删除评论")
    @DeleteMapping("{commentId}")
    public ResultEntity<EmptyObject> deleteCommentById(@PathVariable String commentId) {
        commentService.deleteCommentById(commentId);
        return ResultEntity.SUCCESS(EmptyObject.empty);
    }

    @TokenAuthentication
    @ApiOperation("根据评论ID进行评论点赞")
    @PutMapping("thumpUp/{userId}/{commentId}")
    public ResultEntity<String> thumpUp(@PathVariable String userId,
                                        @PathVariable String commentId) {
        boolean result = commentService.thumpUp(userId, commentId);
        if (result) {
            return ResultEntity.SUCCESS("评论修改成功");
        } else {
            return ResultEntity.FAIL("不能重复评论");
        }
    }
}
